package com.sun.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sun.model.Dept;
import com.sun.service.DeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGetDept")
    public Dept getDeptById(@PathVariable("id") int id) {
        Dept dept = deptService.getById(id);
        if (dept == null) {
            throw new RuntimeException("id=>" + id + "不存在该用户！");
        }
        return dept;
    }


    //备选方案(上述服务调用失败时转到这个服务)
    public Dept hystrixGetDept(@PathVariable("id") int id){
        return new Dept().setDeptNo(id).setDeptName("该用户不存在").setDeptSource("未知信息！");
    }

}
